﻿Imports System.Data.OleDb
Imports System.Data.SqlClient


Public Class DataProvider

    Private stringConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\QLCB.accdb;Persist Security Info=True;Jet OLEDB:Database Password=123!@#"
    Protected cnn As New OleDbConnection
    Private command As New OleDbCommand
    Private reader As OleDb.OleDbDataReader
    Private cmd As OleDbCommand


    Public Sub New()
        cnn = New OleDbConnection(Me.stringConnect)
        command.Connection = cnn
    End Sub

    Public Function GetData(ByVal str_sql As String) As OleDbDataReader
        command.CommandText = str_sql
        Try
            If (cnn.State = ConnectionState.Closed) Then
                cnn.Open()
            End If
            reader = command.ExecuteReader
        Catch ex As Exception
            LOG.Log.AddLog(ex.ToString)
            Return (Nothing)
        End Try
        Return reader
    End Function

    Public Function SaveData(ByVal str_sql As String) As Integer
        command.CommandText = str_sql
        Dim number As Integer
        Try
            If (cnn.State = ConnectionState.Closed) Then
                cnn.Open()
            End If
            number = command.ExecuteNonQuery()
            cnn.Close()
        Catch ex As Exception
            LOG.Log.AddLog(ex.ToString)
            Return (Nothing)
        End Try
        Return number
    End Function

    Public Function GetDT(ByVal str As String) As DataSet
        Dim ds As New DataSet
        Try
            If (cnn.State = ConnectionState.Closed) Then
                cnn.Open()
            End If
            Dim da As New OleDbDataAdapter(str, cnn)

            ds.Clear()
            da.Fill(ds)
            cnn.Close()
        Catch ex As Exception
            LOG.Log.AddLog(ex.ToString)
            Return Nothing
        End Try
        Return ds
    End Function

    Public Function GetValue(ByVal str_sql As String) As String
        command.CommandText = str_sql
        Dim ob As Object
        Try
            If (cnn.State = ConnectionState.Closed) Then
                cnn.Open()
            End If
            ob = command.ExecuteScalar()
            cnn.Close()
        Catch ex As Exception
            LOG.Log.AddLog(ex.ToString)
            Return (Nothing)
        End Try
        Return ob
    End Function

End Class